#include <iostream>
#include <memory>

#include "glm\glm.hpp"
#include "glm\gtx\quaternion.hpp"

#include "GL\glew.h"
#include "WRenderer.h"
#include "WSceneRenderer.h"
#include "WLoader.h"

#include <SFML\Window.hpp>
#include <SFML\System.hpp>

#include "Settings.h"

#include "main.h"

bool running = false;
//bool initiated = false;

std::shared_ptr<WRenderer> wRenderer = nullptr;

#define PI 3.1415926

void InitRenderThread() 
{
	wRenderer = std::make_shared<WSceneRenderer>(running);
	wRenderer->Init();

	
	//std::shared_ptr<WLoadItem> item = std::shared_ptr<WLoadItem>(new WLoadItem(std::string(".\\Data\\Monkey\\monkey.dae")));
	//wRenderer->AddWLoadItem(item);


	wRenderer->Run();
}

void MainLoop()
{
	while(true)
	{
		if(running)
		{
			/*if(!initiated)
			{
				if(wRenderer != nullptr && wRenderer->ContextCreated())
				{
					std::cout<<"Finished Loading"<<std::endl;
					initiated = true;
				}
			}*/
		}
		else 
		{
			break;
		}
	}
}

void Cleanup(sf::Thread& renderThread)
{
	renderThread.terminate();
	wRenderer = nullptr;
	//wLoader = nullptr;
}

void TestQuat()
{
	/*glm::quat q1 = glm::quat(1, 0, 0, 0);
	glm::vec3 v = glm::vec3(1, 0, 0);
	glm::quat inverseQ1 = glm::inverse(q1);

	glm::vec3 rotatedV = q1 * v * inverseQ1;

	float roll = glm::radians(45.0f/2.0f);
	float pitch = glm::radians(0.0f/2.0f);
	float yaw = glm::radians(0.0f/2.0f);

	glm::vec3 eulerAngles(0, 0, 45);
	glm::quat testQ = glm::quat(eulerAngles);

	float testX = cos(roll);
	float testZ = sin(roll);
	
	glm::quat rollQ = glm::quat(cos(roll), 0, 0, sin(roll));
	glm::quat pitchQ = glm::quat(cos(pitch), sin(pitch), 0, 0);
	glm::quat yawQ = glm::quat(cos(yaw), 0, sin(yaw), 0);

	glm::quat invRollQ = glm::inverse(rollQ);
	glm::quat invPitchQ = glm::inverse(pitchQ);
	glm::quat invYawQ = glm::inverse(yawQ);

	glm::vec3 rotatedV2 = invRollQ * (rollQ * v); //rotated to and back again
	*/
}

void main()
{
	running = true;
	TestQuat();
	
	sf::Thread thread(&InitRenderThread);
	thread.launch();

	MainLoop();

	Cleanup(thread);
}